package com.neusoft.elmboot.mapper;

import java.util.List;
import org.apache.ibatis.annotations.*;
import com.neusoft.elmboot.po.Orders;

@Mapper
public interface OrdersMapper {

	@Insert("insert into orders(userId,businessId,orderDate,orderTotal,daId,orderState) values(#{userId},#{businessId},#{orderDate},#{orderTotal},#{daId},0)")
	@Options(useGeneratedKeys=true, keyProperty="orderId", keyColumn="orderId")
	int saveOrders(Orders orders);

	@Select("SELECT * FROM orders WHERE orderId = #{orderId}")
	@Results(id = "ordersResultMap", value = {
			@Result(column = "orderId", property = "orderId", id = true),  // 使用 MyBatis 的 id = true 标识主键
			@Result(column = "userId", property = "userId"),
			@Result(column = "businessId", property = "businessId"),
			@Result(column = "orderDate", property = "orderDate"),
			@Result(column = "orderTotal", property = "orderTotal"),
			@Result(column = "daId", property = "daId"),
			@Result(column = "orderState", property = "orderState"),
			@Result(property = "business", column = "businessId",
					one = @One(select = "com.neusoft.elmboot.mapper.BusinessMapper.getBusinessById")),
			@Result(property = "list", column = "orderId",
					many = @Many(select = "com.neusoft.elmboot.mapper.OrderDetailetMapper.listOrderDetailetByOrderId"))
	})
	Orders getOrdersById(Integer orderId);

	@Select("SELECT * FROM orders WHERE userId = #{userId} ORDER BY orderId")
	@ResultMap("ordersResultMap")
	List<Orders> listOrdersByUserId(String userId);

	@Update("UPDATE orders SET orderState = #{orderState} WHERE orderId = #{orderId}")
	int updateOrderState(@Param("orderId") Long orderId, @Param("orderState") Integer orderState);
}
